/*
 * @Author: 王炜皓
 * @Date: 2023-07-29 16:04:23
 * @LastEditTime: 2023-07-29 16:08:09
 * @LastEditors: 王炜皓
 * @Description: 风场监听事件
 * @FilePath: \cesium-tool\src\views\test\extend\effect\wind\HandlerEvents.ts
 */

import WindyEffect from './windEffect';
 
const Cesium = window.Cesium;
 
export default class HandlerEvents {
  viewer: any;
  windy: WindyEffect;
  handler: any;
  mapExtent: number[];
  render: Function | undefined;
  constructor(viewer: any, obj: WindyEffect) {
    this.viewer = viewer;
    this.windy = obj;
    this.mapExtent = [];
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    this.initStatus();
    this.initHandler(this.windy);
  }
  initHandler(windy: WindyEffect) {
    const $this = this;
    let mouseDown = false;
    let mouseMove = false;
    const canvas = windy.canvas;
 
    //鼠标滚轮事件
    this.handler.setInputAction(() => {
      $this.hideWindy(canvas);
      const refreshTimer = setTimeout(() => {
        windy.extent = $this.mapExtent;
        windy.refresh();
        $this.showWindy(canvas);
        clearTimeout(refreshTimer);
      }, 200);
    }, Cesium.ScreenSpaceEventType.WHEEL);
 
    // 鼠标左键摁下事件
    this.handler.setInputAction(() => {
      mouseDown = true;
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
 
    // 鼠标右键摁下事件
    this.handler.setInputAction(() => {
      mouseDown = true;
    }, Cesium.ScreenSpaceEventType.RIGHT_DOWN);
 
    // 鼠标移动事件
    this.handler.setInputAction(() => {
      if (mouseDown) {
        $this.hideWindy(canvas);
        mouseMove = true;
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
 
    // 鼠标左键抬起事件
    this.handler.setInputAction(() => {
      handlerUp();
    }, Cesium.ScreenSpaceEventType.LEFT_UP);
 
    // 鼠标右键抬起事件
    this.handler.setInputAction(() => {
      handlerUp();
    }, Cesium.ScreenSpaceEventType.RIGHT_UP);
    const handlerUp = () => {
      if (mouseDown && mouseMove) {
        windy.extent = $this.mapExtent;
        windy.refresh();
      }
      $this.showWindy(canvas);
      mouseDown = false;
      mouseMove = false;
    };
  }
 
  initStatus() {
    const $this = this;
    this.resizeCanvas();
    window.onresize = this.resizeCanvas;
    this.render = () => {
      $this.getCesiumExtent();
    };
    // 开启监听器--在渲染场景后立即触发
    this.viewer.scene.postRender.addEventListener(this.render);
  }
  // 显示
  showWindy(ele: HTMLCanvasElement) {
    ele.style.visibility = 'visible';
  }
 
  // 隐藏
  hideWindy(ele: HTMLCanvasElement) {
    ele.style.visibility = 'hidden';
  }
 
  pickPos(viewer: any, p: any) {
    return viewer.scene.globe.pick(viewer.camera.getPickRay(p), viewer.scene);
  }
 
  /**
   * 获取当前地图窗口四至坐标（左上、右上、左下、右下）
   */
  getCesiumExtent() {
    const viewer = this.viewer;
    const canvaswidth = viewer.container.offsetWidth;
    const canvasheight = viewer.container.offseHeight;
    const LT = new Cesium.Cartesian2(0, 0);
    const LB = new Cesium.Cartesian2(0, canvasheight);
    const RT = new Cesium.Cartesian2(canvaswidth, 0);
    const RB = new Cesium.Cartesian2(canvaswidth, canvasheight);
    const pick1 = this.pickPos(viewer, LT);
    const pick2 = this.pickPos(viewer, LB);
    const pick3 = this.pickPos(viewer, RT);
    const pick4 = this.pickPos(viewer, RB);
    if (pick1 && pick2 && pick3 && pick4) {
      // 将三维坐标转成地理坐标（经纬度）---只需计算左下右上的坐标即可
      const ellipsoid = viewer.scene.globe.ellipsoid;
      const pos1 = ellipsoid.cartesianToCartographic(pick2);
      const pos2 = ellipsoid.cartesianToCartographic(pick3);
      const point1 = [
        (pos1.longitude / Math.PI) * 180,
        (pos1.latitude / Math.PI) * 180,
      ];
      const point2 = [
        (pos2.longitude / Math.PI) * 180,
        (pos2.latitude / Math.PI) * 180,
      ];
      // 此时说明extent需要分为东西半球
      if (point1[0] > point2[0]) {
        this.mapExtent = [
          point1[0],
          180,
          point1[1],
          point2[1],
          -180,
          point2[0],
          point1[1],
          point2[1],
        ];
      } else {
        this.mapExtent = [point1[0], point2[0], point1[1], point2[1]];
      }
    } else {
      this.mapExtent = [];
    }
  }
  resizeCanvas() {
    const windycanvas = this.windy.canvas;
    if (!windycanvas) return;
    windycanvas.width = this.viewer.container.offsetWidth;
    windycanvas.height = this.viewer.container.offsetHeight;
    this.windy && this.windy.resize(windycanvas.width, windycanvas.height);
  }
  /**
   * 注销
   */
  destroy() {
    this.handler.destroy();
    this.viewer.scene.postRender.removeEventListener(this.render);
  }
}